package common.mq.master.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;

import common.mq.master.service.IMasterService;
import common.mq.persistence.po.TableName;
import common.mq.persistence.service.IPersistenceService;
import common.mq.po.Queue;

public class MasterServiceImpl implements IMasterService {

	private Map<Integer, Queue> queues = new ConcurrentHashMap<Integer, Queue>(); // queueId-queue
																					// queueId,consumerId一致
	@Autowired
	private IPersistenceService persistenceService ;
	
	public void setPersistenceService(IPersistenceService persistenceService) {
		this.persistenceService = persistenceService;
	}

	@Override
	public Queue getQueue(Integer queueId) {
		return queues.get(queueId);
	}

	@Override
	public Map<Integer, Queue> getQueues() {
		return queues;
	}

	@Override
	public void start() {
		startAllThread();
		loadQueues();
		
	}

	@Override
	public void stop() {
		stopAllThread();
		saveQueues();
	}
	
	@Override
	public void loadQueues(){
		queues = persistenceService.recover(TableName.QUEUE.getName(), Integer.class, Queue.class);
	}
	
	private void saveQueues(){
		Map<String,Map<Integer, Queue>> queueMap = new HashMap<String, Map<Integer,Queue>>();
		queueMap.put(TableName.QUEUE.getName(), queues);
		persistenceService.save(queueMap,false);
	}
	
	private void startAllThread(){
		// TODO
	} 
	
	private void stopAllThread(){
		// TODO
	}
	
	
}
